package com.burgeon.backend.EnterpriseWeChat.rest.Analysis;

import com.burgeon.backend.EnterpriseWeChat.entity.DimDef;
import com.burgeon.backend.EnterpriseWeChat.entity.VipRate;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 购物车推荐
 */
public class getVipRate extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public getVipRate(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public VipRate getData(String storeId) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(storeId), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
//        ArrayList<DimDef> dimDefs= new ArrayList<>();
        VipRate vipRate = new VipRate();
        if(jsonArray.length()>0){
            for (int i = 0; i <jsonArray.length(); i++) {
                vipRate.setTotalQty(jsonArray.getJSONArray(i).getString(0));
                vipRate.setTodayQTY(jsonArray.getJSONArray(i).getString(1));
                vipRate.setTodayVipRate(jsonArray.getJSONArray(i).getString(2));
                vipRate.setYesterdayQTY(jsonArray.getJSONArray(i).getString(3));
                vipRate.setYesterdayVipRate(jsonArray.getJSONArray(i).getString(4));
                vipRate.setMonthQTY(jsonArray.getJSONArray(i).getString(5));
                vipRate.setMonthVipRate(jsonArray.getJSONArray(i).getString( 6));
                vipRate.setLastMonthQTY(jsonArray.getJSONArray(i).getString(7));
                vipRate.setLastMonthVipRate(jsonArray.getJSONArray(i).getString(8));
                vipRate.setTodayVipQTY(jsonArray.getJSONArray(i).getString(9));
                vipRate.setYesterdayVipQTY(jsonArray.getJSONArray(i).getString(10));
                vipRate.setMonthVipQTY(jsonArray.getJSONArray(i).getString(11));
                vipRate.setLastMonthVipQTY(jsonArray.getJSONArray(i).getString(12));
                System.err.println(vipRate);
            }
        }
        return vipRate;
    }

    private MultiValueMap Data(String storeId) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("TOT_QTY");//会员总数
        colums.add("TODAY_QTY");//今日新增
        colums.add("T_VIP_RATE");//今日会销率

        colums.add("YESTERDAY_QTY");//昨日新增
        colums.add("Y_VIP_RATE");//昨日会销率

        colums.add("MONTH_QTY");//本月新增
        colums.add("S_VIP_RATE");//本月会销率

        colums.add("LAST_MONTH_QTY");//上月会销率
        colums.add("L_VIP_RATE");//上月会销率


        colums.add("T_VIPQTY");//今日消费会员
        colums.add("Y_VIPQTY");///昨日消费会员
        colums.add("M_VIPQTY");//本月消费会员
        colums.add("LM_VIPQTY");//上月消费会员

        params.put("columns", colums);
        params.put("table", "VIP_RATESTORE_NO2");//表名
        params.put("count", "true");

        HashMap paramss = new HashMap();

        paramss.put("column","'C_STORE_ID'");
        String a = "'"+storeId+"'";
        paramss.put("condition",a);

//        HashMap orderby = new HashMap();
//
//        orderby.put("column", "ATTRIBNAME");
//        orderby.put("asc", "true");
//        ArrayList list = new ArrayList();
//        list.add(orderby);
//
//        params.put("orderby", list);

        params.put("params", paramss);
//        System.err.println(paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        getVipRate g = new getVipRate("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("3128"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
